package com.redrose.flutterdemo.flutter;

import com.alibaba.android.arouter.launcher.ARouter;
import com.redrose.flutterdemo.rxbus.RxBus;
import com.redrose.flutterdemo.rxbus.RxSubscriptions;
import com.redrose.flutterdemo.rxbus.event.MenuSwitchEvent;
import com.redrose.flutterdemo.ui.activitys.BaseActivity;
import com.redrose.flutterdemo.ui.fragments.BaseFragment;
import com.redrose.flutterdemo.utils.LogUtils;
import com.redrose.flutterdemo.utils.ToastUtil;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.reactivex.functions.Consumer;

/**
 * Desc:
 *
 * @author: RedRose
 * Date: 2019/5/24
 * Email: yinsxi@163.com
 */
public class FlutterHandler implements MethodChannel.MethodCallHandler, EventChannel.StreamHandler {

    private BaseFragment mFragment;
    private BaseActivity mActivity;

    public FlutterHandler(BaseFragment fragment) {
        mFragment = fragment;
    }

    public FlutterHandler(BaseActivity activity) {
        mActivity = activity;
    }

    /**
     * 原生调用 Flutter端
     */
    @Override
    public void onListen(Object o, EventChannel.EventSink eventSink) {
        RxSubscriptions.add(RxBus.getDefault().toObservable(MenuSwitchEvent.class).subscribe(
                new Consumer<MenuSwitchEvent>() {
                    @Override
                    public void accept(MenuSwitchEvent menuSwitchEvent) throws Exception {
                        if (menuSwitchEvent != null) {
                            int positon = menuSwitchEvent.getPosition();
                            switch (positon) {
                                case 0:
                                    break;
                                case 1:
                                    break;
                                case 2:
                                    break;
                                case 3:
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                }
        ));
    }

    @Override
    public void onCancel(Object o) {

    }

    /**
     * Flutter 调用 原生
     */
    @Override
    public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
        String method = methodCall.method;
        switch (method) {
            //调用原生登录
            case "login":

                break;
            //调用原生退出登录
            case "logout":

                break;
            case "getSystemData":
                ToastUtil.show(mFragment.getContext(), "收到Flutter 调用Android");
                break;
            case "openCamera":
                ARouter.getInstance()
                        .build("/activity/camera")
                        .navigation();
                break;
            default:
                break;
        }
    }
}
